module main

import os
import json
import net
import rand
import dialog
import ttytm.webview

struct Windows {
	icon   string
	title  string
	width  int
	height int
	hint   string
	debug  bool
}

struct Php {
	file                  string
	entrance              string
	address               string
	port                  string
	is_custom_instruction bool
	start_instruction     string
	stop_instruction      string
	auto_stop             bool
}

struct Config {
	windows Windows
	php     Php
}

enum Hint {
	// Width and height are default size.
	@none = C.WEBVIEW_HINT_NONE
	// Window size can not be changed by a user.
	fixed = C.WEBVIEW_HINT_FIXED
	// Width and height are minimum bounds.
	min   = C.WEBVIEW_HINT_MIN
	// Width and height are maximum bounds.
	max   = C.WEBVIEW_HINT_MAX
}

// 窗口hint
pub fn hint_val(h string) webview.Hint {
	match h {
		'auto' {
			return .@none
		}
		'fixed' {
			return .fixed
		}
		'min' {
			return .min
		}
		'max' {
			return .max
		}
		else {
			return .@none
		}
	}
}

// 随即端口
pub fn rand_port() !u16 {
	p := rand.int_in_range(2, 9999) or { return 9999 }
	return net.validate_port(p)!
}

// 获取端口
fn get_port(port string) !u16 {
	if port == '*' {
		return rand_port()!
	}
	return port.u16()
}

// 弹出消息窗口
fn win_alert(e &webview.Event) bool {
	str_arg := e.get_arg[string](0) or { '' }
	num_arg := e.get_arg[int](1) or { 0 }
	match num_arg {
		0 {
			return dialog.message(str_arg)
		}
		1 {
			return dialog.message(str_arg, buttons: .ok_cancel)
		}
		else {
			return dialog.message(str_arg, buttons: .yes_no)
		}
	}
}

// 打开文件框
fn win_file_dialog(e &webview.Event) string {
	f := dialog.file_dialog() or { '' }
	return f.str()
}

// 打开文件夹
fn win_open_dir(e &webview.Event) string {
	str_arg := e.get_arg[string](0) or { '' }
	f := dialog.FileOpenOptions{str_arg}
	p, _ := dialog.open_dir(f) or { return '' }
	return p
}

fn main() {
	// 获取json字符串
	data := os.read_file('./config.json')!
	// 转义json
	config_json := json.decode(Config, data)!

	// 创建窗口
	w := webview.create(debug: config_json.windows.debug)
	// 设置图标
	w.set_icon(config_json.windows.icon) or {}
	// 设置标题
	w.set_title(config_json.windows.title)
	// 设置大小
	w.set_size(config_json.windows.width, config_json.windows.height, hint_val(config_json.windows.hint))
	// 绑定
	w.bind('win_alert', win_alert)

	w.bind('win_file_dialog', win_file_dialog)

	w.bind('win_open_dir', win_open_dir)
	// 获取端口号
	port := get_port(config_json.php.port)!
	// 运行php
	newname := config_json.php.file
	mut newargs := []string{}
	if config_json.php.is_custom_instruction {
		newargs << config_json.php.start_instruction.split(' ')
	} else {
		newargs << ['-S', '${config_json.php.address}:${port}', config_json.php.entrance]
	}
	mut p := os.new_process(newname)
	p.create_no_window = true
	p.set_args(newargs) // 设置参数
	p.run()
	// 设置nav
	w.navigate('http://${config_json.php.address}:${port}')
	// 运行
	w.run()
	// 结束php
	if config_json.php.auto_stop {
		p.signal_term()
	} else {
		os.system(config_json.php.stop_instruction)
	}
	// 结束窗口
	w.destroy()
}
